﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace CircleWindow
{
    public class MyCirclePanel : Panel
    {
        public double Diameter
        {
            get { return (double)GetValue(DiameterProperty); }
            set { SetValue(DiameterProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Diameter.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DiameterProperty =
            DependencyProperty.Register("Diameter", typeof(double), typeof(MyCirclePanel), new PropertyMetadata(0.0));
        
        public bool KeepVertical
        {
            get { return (bool)GetValue(KeepVerticalProperty); }
            set { SetValue(KeepVerticalProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Diameter.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty KeepVerticalProperty =
            DependencyProperty.Register("KeepVertical", typeof(bool), typeof(MyCirclePanel), new PropertyMetadata(false));


        public double OffsetAngle
        {
            get { return (double)GetValue(OffsetAngleProperty); }
            set { SetValue(OffsetAngleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Diameter.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OffsetAngleProperty =
            DependencyProperty.Register("OffsetAngle", typeof(double), typeof(MyCirclePanel), new PropertyMetadata(0.0));

        protected override Size MeasureOverride(Size availableSize)
        {
            double diameter = Diameter;
            if (base.Children.Count == 0)
            {
                return new Size(diameter, diameter);
            }

            Size size = new Size(diameter, diameter);
            foreach (UIElement child in base.Children)
            {
                if (child.IsVisible != false)
                    child.Measure(size);
            }

            return size;
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            bool keepVertical = KeepVertical;
            double offsetAngle = OffsetAngle;
            int num = 0;
            int avalidCount = 0;
            foreach (UIElement child in base.Children)
                if (child.IsVisible != false)
                    avalidCount++;
            //double num2 = 360.0 / (double)base.Children.Count;
            double num2 = 360.0 / (double)avalidCount;
            double num3 = Diameter / 2.0;
            foreach (UIElement child in base.Children)
            {
                if (child.IsVisible == false)
                    continue;
                double num4 = child.DesiredSize.Width / 2.0;
                double num5 = child.DesiredSize.Height / 2.0;
                double num6 = num2 * (double)num++ + offsetAngle;
                RotateTransform rotateTransform = (RotateTransform)(child.RenderTransform = new RotateTransform
                {
                    CenterX = num4,
                    CenterY = num5,
                    Angle = (keepVertical ? 0.0 : num6)
                });
                double num7 = Math.PI * num6 / 180.0;
                double num8 = num3 * Math.Cos(num7);
                double num9 = num3 * Math.Sin(num7);
                double x = num8 + finalSize.Width / 2.0 - num4;
                double y = num9 + finalSize.Height / 2.0 - num5;
                child.Arrange(new Rect(x, y, child.DesiredSize.Width, child.DesiredSize.Height));
            }

            return finalSize;
        }
    }
}
